package com.example.shoppingmall.cotroller;

import com.example.shoppingmall.pojo.Note;
import com.example.shoppingmall.pojo.Page;
import com.example.shoppingmall.service.NoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class NoteController {

    @Autowired
    private NoteService noteService;

    public void setNoteService(NoteService noteService) {
        this.noteService = noteService;
    }

    /**
     * 查询
     * @param phone
     * @param start
     * @param end
     * @param no
     * @param session
     * @param model
     * @return
     */
    @RequestMapping(value = "/toNoteList")
    public String toNoteList(@RequestParam(value = "phone",required = false)String phone, @RequestParam(value = "start",required = false)String start,
                             @RequestParam(value = "end",required = false)String end, @RequestParam(value = "no",required = false)String no,
                             HttpSession session, Model model){
        model.addAttribute("phone",phone);
        model.addAttribute("start",start);
        model.addAttribute("end",end);
        int curr = 1;
        Page page = new Page();
        int count = noteService.count(phone,start,end);
        page.setCount(count);
        if (no != null) {
            curr = Integer.parseInt(no);
            if (curr > page.getPageSum()) {
                curr = page.getPageSum();
            } else if (curr < 1) {
                curr = 1;
            }
        }
        page.setPageNo(curr);
        List<Note>noteList=noteService.noteList(phone,start,end,(curr-1)*6);
        session.setAttribute("page", page);
        session.setAttribute("noteList", noteList);
        return "user/noteList";
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/toNoteDelete")
    public String toAdminDelete(@RequestParam("id") String id) {
        int rs = noteService.delete(Integer.parseInt(id));
        if (rs > 0) {
            return "redirect:toNoteList";
        } else {
            return "redirect:toNoteList";
        }
    }
}
